<?php
if (isset($_SESSION['openid']) &&  isset($_POST['id']) ){
    include '../c/config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
    $openid=$_SESSION['openid'];
    $cday=date("md");
    $query=$mysqli->query("select count(id) as count from chou where openid='".$openid."' and cday='".$cday."' limit 0,3 ");
    $getData=$query->fetch_array();
    $count=$getData['count'];
    if($count < 3){
        $runRand=rand(2,3);
        $jp=5;
        $pic=$jp;
        if(3 == $count){
            $runRand=2;
        }
        $query=$mysqli->query("select id from chou where openid='".$openid."' and (jp=4 or jp=2 or jp=3 or jp=1)  limit 0, 1");
        if($query->num_rows>0){ 
            $runRand=1;
        }
        if (1 != $runRand){
            $mysqli->query("insert into j_log(openid,createtime) values('".$openid."','".date("Y-m-d H:i:s")."')");
            $query=$mysqli->query("select id from j_log where openid='".$openid."' order by id desc  limit 0,1 ");
            $getJLog=$query->fetch_array();
            $queryChi=$mysqli->query("select qu,jp from chi where id='".$getJLog['id']."' limit 0,1 ");
            if($queryChi->num_rows>0){
                $chiInfo=$queryChi->fetch_array();
                $jp=$chiInfo['jp'];
                $qu=$chiInfo['qu'];
                if ('zj' == $qu){
                	$queryUser=$mysqli->query("select sheng from uinfo where openid='".$openid."' limit 0,1 ");
                	$getInfo=$queryUser->fetch_array();
                	$usheng=$getInfo['sheng'];
                	if('浙江省'  != $usheng){
                		$jp=5;
                	}
                }
                $pic=$jp;
            }
        }
       
        $mysqli->query("insert into chou(openid,jp,cday,createtime) values('".$openid."','".$jp."','".$cday."','".date("Y-m-d H:i:s")."')  ");
        echo json_encode(array('status'=>'work', 'l'=> $jp, 'p'=>$pic,'q'=>$runRand));
    }else{
        echo json_encode(array('status'=>'not'));
    }
}else{
    echo json_encode(array('status'=>'not'));
}